package com.ruoyi.gzmx_scdw.controller;

import java.util.*;

import com.ruoyi.Gzmx_device.domain.Gzmx_device;
import com.ruoyi.gzmx_scdw.domain.ZtreeNew;
import com.ruoyi.gzmx_tag.domain.Gzmx_tag;
import com.ruoyi.youtian.domain.TreeSelect;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gzmx_scdw.domain.Gzmx_scdw;
import com.ruoyi.gzmx_scdw.service.IGzmx_scdwService;
import com.ruoyi.gzmx_tag_current.domain.Gzmx_tag_current;
import com.ruoyi.gzmx_tag_current.service.IGzmx_tag_currentService;
import com.ruoyi.gzmx_tag_history.domain.Gzmx_tag_history;
import com.ruoyi.gzmx_tag_history.service.IGzmx_tag_historyService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gzmx.in_phd.in_phd;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SysDept;

/**
 * gzmx_scdwController
 *
 * @author ruoyi
 * @date 2022-07-25
 */
@Component("gzmx_scdw")
@Controller
@RequestMapping("/gzmx_scdw/gzmx_scdw")
public class Gzmx_scdwController extends BaseController
{
    private String prefix = "gzmx_scdw/gzmx_scdw";

    @Autowired
    private IGzmx_scdwService gzmx_scdwService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private IGzmx_tag_historyService gzmx_tag_historyService;
    
    @Autowired
    private IGzmx_tag_currentService gzmx_tag_currentService;

    @RequiresPermissions("gzmx_scdw:gzmx_scdw:view")
    @GetMapping()
    public String gzmx_scdw()
    {
    	//post_phd();

    	return prefix + "/gzmx_scdw";
    }

    /**
     * 查询gzmx_scdw树列表
     */
    @RequiresPermissions("gzmx_scdw:gzmx_scdw:list")
    @PostMapping("/list")
    @ResponseBody
    public List<Gzmx_scdw> list(Gzmx_scdw gzmx_scdw)
    {

    	SysDept dept=new SysDept();
    	dept.setAncestors("185");
    	List<SysDept> list_dept=sysDeptService.selectChildrenDeptById(185L);

    	List<Gzmx_scdw> list = gzmx_scdwService.selectGzmx_scdwList(gzmx_scdw);
    	//List<Gzmx_scdw> list= new ArrayList<Gzmx_scdw>();
    	/*
    	for(int i=0;i<list.size();i++) {
    		Gzmx_scdw gzmx_scdw_new=new Gzmx_scdw();
    		gzmx_scdw_new.setDept_id_id(list.get(i).getParent_id());
    		List<Gzmx_scdw> list_new=gzmx_scdwService.selectGzmx_scdwList(gzmx_scdw_new);
    		if(list_new.size()>0) {
    			list.get(i).setFdwdm(list_new.get(0).getId());
    			//gzmx_scdwService.updateGzmx_scdw(list.get(i));
    		}

    	}

        for(int i=0;i<list_dept.size();i++) {
        	Gzmx_scdw gzmx_scdw_new=new Gzmx_scdw();
        	gzmx_scdw_new.setId(Long.valueOf(list_dept.get(i).getDeptId()));
        	gzmx_scdw_new.setDwmc(list_dept.get(i).getDeptName());

        	if(Long.valueOf(list_dept.get(i).getParentId())==185L) {
        		gzmx_scdw_new.setFdwdm(null);
        		gzmx_scdw_new.setParent_id(list_dept.get(i).getParentId());
        		gzmx_scdw_new.setDept_id_id(list_dept.get(i).getDeptId());
        	}else {
        		gzmx_scdw_new.setFdwdm(list_dept.get(i).getParentId());
        		gzmx_scdw_new.setParent_id(list_dept.get(i).getParentId());
        		gzmx_scdw_new.setDept_id_id(list_dept.get(i).getDeptId());
        	}

        	if(list_dept.get(i).getAncestors().split(",").length>4) {

        		gzmx_scdw_new.setDeptId(Long.valueOf(list_dept.get(i).getAncestors().split(",")[4]));
        		gzmx_scdw_new.setBm_id(list_dept.get(i).getDeptId());
        		gzmx_scdw_new.setBm_name(list_dept.get(i).getDeptName());
        	}else {

        		gzmx_scdw_new.setDeptId(list_dept.get(i).getDeptId());


        	}



        	//gzmx_scdwService.insertGzmx_scdw(gzmx_scdw_new);
        	//list.add(gzmx_scdw_new);
    	}
       */
        return list;
    }


    /**
     * 查询点位级别列表
     * @param type  1-5级
     * @param parentId  上级ID
     * @return
     */
//    @RequiresPermissions("gzmx_scdw:gzmx_scdw:reolist")
    @GetMapping("/sourceLevelist")
    @ResponseBody
    public List<Map<Long,String>> sourceLevelist(Integer type, Long parentId)
    {
        List<Map<Long,String>> result=new ArrayList<>();
        Gzmx_tag  tag=new Gzmx_tag();
        switch (type){
            case 1:
                break;
            case 2:
                tag.setZyq_id(parentId);
                break;
            case 3:
                tag.setDw_3_id(parentId);
                break;
            case 4:
                tag.setDw_4_id(parentId);
                break;
            case 5:
                tag.setDw_5_id(parentId);
                break;


        }


        List<Gzmx_tag> list = gzmx_scdwService.reselectGzmx_reoscdwList(tag);

        Map<Long,String> tmp=new HashMap<>();

        for (Gzmx_tag gzmx_tag : list) {

            switch (type){
                case 1:
                    if(!tmp.containsKey(gzmx_tag.getZyq_id())){
                        tmp.put(gzmx_tag.getZyq_id(),gzmx_tag.getZyq_name());
                    }
                    break;
                case 2:

                    if(!tmp.containsKey(gzmx_tag.getDw_3_id())){
                        tmp.put(gzmx_tag.getDw_3_id(),gzmx_tag.getDw_3_name());
                    }
                    break;
                case 3:

                    if(!tmp.containsKey(gzmx_tag.getDw_4_id())){
                        tmp.put(gzmx_tag.getDw_4_id(),gzmx_tag.getDw_4_name());
                    }
                    break;
                case 4:

                    if(!tmp.containsKey(gzmx_tag.getDw_5_id())){
                        tmp.put(gzmx_tag.getDw_5_id(),gzmx_tag.getDw_5_name());
                    }
                    break;
                case 5:

                    if(!tmp.containsKey(gzmx_tag.getFdwdm())){
                        tmp.put(gzmx_tag.getFdwdm(),gzmx_tag.getFdwdm_name());
                    }
                    break;

            }
        }

        tmp.forEach((id,name)->{
            result.add(new HashMap(){{
                put("id",id);
                put("name",name);
            }});
        });





        return result;
    }

    /**
     * 查询点位列表
     * @param parentId 上级ID
     * @return
     */
    @GetMapping("/sourcelist")
    @ResponseBody
    public List<Map<Long,String>> sourcelist(Integer type,Long parentId)
    {

        List<Map<Long,String>> result=new ArrayList<>();
        Gzmx_tag  tag=new Gzmx_tag();
       if(Objects.nonNull(type)){
           switch (type){
               case 1:
                   tag.setZyq_id(parentId);
                   break;
               case 2:
                   tag.setDw_3_id(parentId);
                   break;
               case 3:
                   tag.setDw_4_id(parentId);
                   break;
               case 4:
                   tag.setDw_5_id(parentId);
                   break;
               case 5:
                   tag.setFdwdm(parentId);
                   break;


           }


       }
        List<Gzmx_tag> list = gzmx_scdwService.reselectGzmx_reoscdwList(tag);
        list.forEach(gzmx_tag -> result.add(new HashMap(){{
            put("id",gzmx_tag.getSourceTag());
            put("name",gzmx_tag.getDescription());
            put("max",gzmx_tag.getMax_value());
            put("min",gzmx_tag.getMin_value());
        }}));
        return result;
    }




    /**
     * 导出gzmx_scdw列表
     */
    @RequiresPermissions("gzmx_scdw:gzmx_scdw:export")
    @Log(title = "gzmx_scdw", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Gzmx_scdw gzmx_scdw)
    {
        List<Gzmx_scdw> list = gzmx_scdwService.selectGzmx_scdwList(gzmx_scdw);
        ExcelUtil<Gzmx_scdw> util = new ExcelUtil<Gzmx_scdw>(Gzmx_scdw.class);
        return util.exportExcel(list, "gzmx_scdw数据");
    }

    /**
     * 新增gzmx_scdw
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {

        	mmap.put("gzmx_scdw", gzmx_scdwService.selectGzmx_scdwById(id));

        }else {

        	Gzmx_scdw gzmx_scdw_a=new Gzmx_scdw();

        	gzmx_scdw_a.setJb(0L);

        	mmap.put("gzmx_scdw", gzmx_scdw_a);
        }
        return prefix + "/add";
    }

    /**
     * 新增保存gzmx_scdw
     */
    @RequiresPermissions("gzmx_scdw:gzmx_scdw:add")
    @Log(title = "gzmx_scdw", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Gzmx_scdw gzmx_scdw)
    {
    	Gzmx_scdw gzmx_scdw_new=gzmx_scdwService.selectGzmx_scdwById(gzmx_scdw.getFdwdm());
    	System.out.println("Gzmx_scdw--"+gzmx_scdw_new);
    	gzmx_scdw.setDeptId(gzmx_scdw_new.getDeptId());
    	gzmx_scdw.setEjdwName(gzmx_scdw_new.getEjdwName());
    	gzmx_scdw.setEjdwId(gzmx_scdw_new.getEjdwId());
    	gzmx_scdw.setZyqName(gzmx_scdw_new.getZyqName());
    	gzmx_scdw.setZyqId(gzmx_scdw_new.getZyqId());
    	if(gzmx_scdw.getJb().longValue()==3) {

    		gzmx_scdw.setDw_3_name(gzmx_scdw.getDwmc());
    	}
    	if(gzmx_scdw.getJb().longValue()==4) {
    		gzmx_scdw.setDw_3_id(gzmx_scdw_new.getDw_3_id());
    		gzmx_scdw.setDw_3_name(gzmx_scdw_new.getDw_3_name());
    		//gzmx_scdw.setDw_4_id(gzmx_scdw_new.getDw_3_id());
    		gzmx_scdw.setDw_4_name(gzmx_scdw.getDwmc());
    	}
    	if(gzmx_scdw.getJb().longValue()==5) {
    		gzmx_scdw.setDw_3_id(gzmx_scdw_new.getDw_3_id());
    		gzmx_scdw.setDw_3_name(gzmx_scdw_new.getDw_3_name());
    		gzmx_scdw.setDw_4_id(gzmx_scdw_new.getDw_4_id());
    		gzmx_scdw.setDw_4_name(gzmx_scdw_new.getDw_4_name());
    		//gzmx_scdw.setDw_5_id(gzmx_scdw_new.getDw_4_id());
    		gzmx_scdw.setDw_5_name(gzmx_scdw.getDwmc());

    	}
    	System.out.println("gzmx_scdw.getDwmc()--"+gzmx_scdw.getDwmc());
    	System.out.println("gzmx_scdw.getDw_5_name()--"+gzmx_scdw.getDw_5_name());
    	gzmx_scdwService.insertGzmx_scdw(gzmx_scdw);

    	if(gzmx_scdw.getJb().longValue()==3) {
    		gzmx_scdw.setDw_3_name(null);
    		gzmx_scdw.setDw_3_id(gzmx_scdw.getId());
    	}

    	if(gzmx_scdw.getJb().longValue()==4) {
    		gzmx_scdw.setDw_4_name(null);
    		gzmx_scdw.setDw_4_id(gzmx_scdw.getId());

    	}
    	if(gzmx_scdw.getJb().longValue()==5) {
    		gzmx_scdw.setDw_4_name(null);
    		gzmx_scdw.setDw_5_name(null);
    		gzmx_scdw.setDw_5_id(gzmx_scdw.getId());

    	}
    	gzmx_scdwService.updateGzmx_scdw(gzmx_scdw);
    	return success();//toAjax(gzmx_scdwService.insertGzmx_scdw(gzmx_scdw));
    }

    /**
     * 修改gzmx_scdw
     */
    @RequiresPermissions("gzmx_scdw:gzmx_scdw:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Gzmx_scdw gzmx_scdw = gzmx_scdwService.selectGzmx_scdwById(id);
        mmap.put("gzmx_scdw", gzmx_scdw);
        return prefix + "/edit";
    }

    /**
     * 修改保存gzmx_scdw
     */
    @RequiresPermissions("gzmx_scdw:gzmx_scdw:edit")
    @Log(title = "gzmx_scdw", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Gzmx_scdw gzmx_scdw)
    {
        return toAjax(gzmx_scdwService.updateGzmx_scdw(gzmx_scdw));
    }

    /**
     * 删除
     */
    @RequiresPermissions("gzmx_scdw:gzmx_scdw:remove")
    @Log(title = "gzmx_scdw", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(gzmx_scdwService.deleteGzmx_scdwById(id));
    }

    /**
     * 选择gzmx_scdw树
     */
    @GetMapping(value = { "/selectGzmx_scdwTree/{id}", "/selectGzmx_scdwTree/" })
    public String selectGzmx_scdwTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("gzmx_scdw", gzmx_scdwService.selectGzmx_scdwById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载gzmx_scdw树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = gzmx_scdwService.selectGzmx_scdwTree();
        return ztrees;
    }

    public String post_phd(String webServiceURL, String qy_cs,String str_password,String username,String soapXml,String soapAction, String soapType) {
    	in_phd in_phd=new in_phd();
    	webServiceURL="http://"+webServiceURL;
    	System.out.println("webServiceURL111----"+webServiceURL);
    	String re=com.ruoyi.gzmx.in_phd.in_phd.doPostSoap(webServiceURL,qy_cs,str_password,username, "", "", "");
    	//System.out.println("re----"+re);
    	List<Gzmx_tag_history> list=in_phd.parseSoapMessage(re);

    	for(int i=0;i<list.size();i++) {
    		Gzmx_tag_current gzmx_tag_current=new Gzmx_tag_current();
    		gzmx_tag_current.setSourceTag(list.get(i).getSourceTag());
    		List<Gzmx_tag_current> list_gzmx_tag_current= gzmx_tag_currentService.selectGzmx_tag_currentList(gzmx_tag_current);
    		if(list_gzmx_tag_current.size()>0) { 
    			
    			list_gzmx_tag_current.get(0).setValue(list.get(i).getValue().longValue());
    			gzmx_tag_currentService.updateGzmx_tag_current(list_gzmx_tag_current.get(0));
    		}else {
    			
    			Gzmx_tag_current gzmx_tag_current_new=new Gzmx_tag_current();
    			gzmx_tag_current_new.setSourceTag(list.get(i).getSourceTag());
    			gzmx_tag_current_new.setSearchValue(list.get(i).getSearchValue());
    			gzmx_tag_current_new.setDescription(list.get(i).getDescription());    		
    			gzmx_tag_current_new.setValue(list.get(i).getValue().longValue());
    			gzmx_tag_currentService.insertGzmx_tag_current(gzmx_tag_current_new);
    			
    		}
    		
    		gzmx_tag_historyService.insertGzmx_tag_history(list.get(i));


    	}


    	//List<Tag_result> list1=in_phd.getPicklistFromSoapResponse(re);

    	System.out.println("liste----"+list.size());
    	//System.out.println("liste----"+list);
    	//System.out.println("liste1----"+list1.size());
    	//System.out.println("liste1----"+list1);

    	return re;
    }

    /**
     *  生产点位树结构
     */
    @RequiresPermissions("Gzmx_device:Gzmx_device:list")
    @GetMapping("/tree")
    @ResponseBody
    public AjaxResult gzmxTree(Gzmx_scdw gzmxScdw)
    {
        List<TreeSelect> treeList = gzmx_scdwService.selectGzmx_scdwTreeSelectList(gzmxScdw);
        return AjaxResult.success(treeList);
    }

    /***
     *  基础管理左侧树
     * @param gzmxScdw
     * @return
     */
    @RequiresPermissions("Gzmx_device:Gzmx_device:list")
    @PostMapping("/basicTree")
    @ResponseBody
    public AjaxResult gzmxBasicTree(Gzmx_scdw gzmxScdw)
    {
        List<TreeSelect> treeList = gzmx_scdwService.selectGzmx_scdwBasicTreeSelectList(gzmxScdw);
        return AjaxResult.success(treeList);
    }

    /**
     * 模型管理左侧树
     * @return
     */
    @GetMapping("/basicTreeData")
    @ResponseBody
    public List<Ztree> basicTreeData()
    {
        List<Ztree> ztrees = gzmx_scdwService.selectGzmx_scdwTreeNew();
        return ztrees;
    }


    /**
     * 场景管理左侧树
     * 左侧三层生产点位+场景树
     * @return
     */
    @GetMapping("/temlateTreeData")
    @ResponseBody
    public List<Ztree> temlateTreeData()
    {
        List<Ztree> ztrees = gzmx_scdwService.temlateTreeData();
        return ztrees;
    }

    /**
     * 场景管理左侧树
     * 左侧三层生产点位+场景树
     * @return
     */
    @GetMapping("/temlateTreeDataNew")
    @ResponseBody
    public List<ZtreeNew> temlateTreeDataNew()
    {
        List<ZtreeNew> ztrees = gzmx_scdwService.templateTreeDataNew();
        return ztrees;
    }

    /**
     * 基础管理 左侧树
     * @return
     */
    @GetMapping("/jichuTreeData")
    @ResponseBody
    public List<Ztree> jichuTreeData()
    {
        List<Ztree> ztrees = gzmx_scdwService.jichuTreeData();
        return ztrees;
    }

    /**
     * 基础管理 左侧树
     * @return
     */
    @GetMapping("/jichuTreeDataNew")
    @ResponseBody
    public List<ZtreeNew> jichuTreeDataNew()
    {
        List<ZtreeNew> ztrees = gzmx_scdwService.jichuTreeDataNew();
        return ztrees;
    }

}
